package com.hlcl.huaji.ctrl.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.elcl.adapter.AbViewHolder;
import com.elcl.adapter.BaseLVAdapter;
import com.elcl.util.DateUtil;
import com.hlcl.huaji.R;
import com.hlcl.huaji.model.Order;
import com.hlcl.huaji.utils.HideUtils;

import java.util.List;

/**
 * Created by lichunlin on 2018/5/21.
 * 首页订单列表内容
 */

public class OrderListAdapter extends BaseLVAdapter<Order> {

    private int orderType;

    public void setOrderType(int orderType) {
        this.orderType = orderType;
    }

    public OrderListAdapter(List<Order> list, int idLayout) {
        super(list, idLayout);
    }

    @Override
    public View getItemView(final int position, View convertView, ViewGroup parent) {
        TextView tv_name = (TextView) AbViewHolder.getView(convertView, R.id.tv_name);
        TextView tv_publish_date = (TextView) AbViewHolder.getView(convertView, R.id.tv_publish_date);
        TextView tv_company = (TextView) AbViewHolder.getView(convertView, R.id.tv_company);
        TextView tv_price = (TextView) AbViewHolder.getView(convertView, R.id.tv_price);
        TextView tv_location = (TextView) AbViewHolder.getView(convertView, R.id.tv_location);
        TextView tv_date = (TextView) AbViewHolder.getView(convertView, R.id.tv_date);
        TextView tv_repair = (TextView) AbViewHolder.getView(convertView, R.id.tv_repair);

        Order order = list.get(position);
        if (null != order) {
            tv_name.setText(order.getCreaterName());
            tv_publish_date.setText(DateUtil.getDateFormat(order.getOrderTime(), DateUtil.YYYY_MM_DD_HH_MM));
            double priceAll = order.getPrice() + order.getAdditionalPrice();
            tv_price.setText("￥" + priceAll);
            if (order.getType().equals("0")) {//快速维修
                tv_date.setText("快速维修");
            } else if (order.getType().equals("1")) {
                tv_date.setText("预约维修|" + DateUtil.getDateFormat(order.getReserveTime(), DateUtil.YYYY_MM_DD_HH_MM));
            }
            tv_repair.setText(order.getRepairContent());
        }

        TextView tv_state = (TextView) AbViewHolder.getView(convertView, R.id.tv_state);

        String company = "";//公司
        String repairAddress = "";//维修地址
        if (orderType == 1 || orderType == 2) {//如果不是最新，不显示
            tv_state.setVisibility(View.VISIBLE);
            company = HideUtils.getHideContent(order.getCreaterCompany());
            repairAddress = HideUtils.getHideContent(order.getRepairAddress());
        } else {
            tv_state.setVisibility(View.GONE);
            company = order.getCreaterCompany();
            repairAddress = order.getRepairAddress();
        }
        tv_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClickListener(position);
            }
        });
        tv_company.setText(company);
        tv_location.setText(repairAddress);

        return convertView;
    }


    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClickListener(int position);
    }
}
